package com.fhz.kotlinmvvm

import android.content.*
import android.os.Bundle
import android.os.IBinder
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.fhz.kotlinmvvm.broadcast.MyReceiver2
import com.fhz.kotlinmvvm.broadcast.MyReceiver3
import com.fhz.kotlinmvvm.con.BROADCAST_ACTION
import com.fhz.kotlinmvvm.con.BROADCAST_ACTION_FENG
import com.fhz.kotlinmvvm.databinding.ActivityMainBinding
import com.fhz.kotlinmvvm.entity.Banner
import com.fhz.kotlinmvvm.entity.Goods
import com.fhz.kotlinmvvm.main.GoodsRVAdapter
import com.fhz.kotlinmvvm.main.MainViewModel
import com.fhz.kotlinmvvm.service.MyIntentService
import com.fhz.kotlinmvvm.service.MyService
import com.fhz.kotlinmvvm.service.MyService.MyBinder
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch


@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

    val mainViewModel:MainViewModel  by viewModels() //viewmodlePervidoer

    lateinit var binding:ActivityMainBinding

    lateinit var receiver: MyReceiver2
    lateinit var receiver3: MyReceiver3

    lateinit var startService1:Intent

    lateinit var goodsAdapter:GoodsRVAdapter
    val list:ArrayList<Banner> = ArrayList<Banner>()
    var goodsList:ArrayList<Goods> = ArrayList<Goods>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)



        //动态注册一个广播接收者,一定要注销
        receiver = MyReceiver2()
        val filter = IntentFilter()
        filter.addAction(BROADCAST_ACTION)
        registerReceiver(receiver,filter)

        receiver3 = MyReceiver3()
        val filter3 = IntentFilter()
        filter3.addAction(BROADCAST_ACTION)
        registerReceiver(receiver3,filter3)


        binding.mainRv.layoutManager = LinearLayoutManager(this)
        goodsAdapter = GoodsRVAdapter(this,goodsList) { goods -> click(goods)}
        binding.mainRv.adapter = goodsAdapter


        lifecycleScope.launch {
            val goodsList1 = mainViewModel.getGoodsList(1, 10)
            goodsList.addAll(goodsList1.data.list)
            goodsAdapter.notifyDataSetChanged()
        }

        lifecycleScope.launchWhenResumed {
            val banners = mainViewModel.getBanners(0, 1, 5)
            println(banners.data.size)
            binding.mainBanner.setAdapter(object : BannerImageAdapter<Banner>(banners.data.list) {
                override fun onBindView(
                    holder: BannerImageHolder,
                    data: Banner,
                    position: Int,
                    size: Int
                ) {
                    //图片加载自己实现
                    Glide.with(holder.itemView)
                        .load(data.imagepath)
                        .apply(RequestOptions.bitmapTransform(RoundedCorners(30)))
                        .into(holder.imageView)
                }
            })
                .addBannerLifecycleObserver(this@MainActivity) //添加生命周期观察者
                .setIndicator(CircleIndicator(this@MainActivity))
        }


    }

    override fun onStart() {
        super.onStart()

    }

    override fun onRestart() {
        super.onRestart()
    }

    override fun onPause() {
        super.onPause()
    }

    override fun onStop() {
        super.onStop()
    }


    private fun click(goods: Goods) {
        println(goods.goodsDesc)

        //下载服务.
//        startService()
//        bindService()

        startService1 = Intent(this,MyService::class.java)
        startService(startService1)
//
//
//        stopService(startService1)

        val connect =  object : ServiceConnection {
            override fun onServiceConnected(name: ComponentName?, service: IBinder?) { //mybinder
                val myBinder = service as MyBinder
                myBinder.callShow()

            }

            override fun onServiceDisconnected(name: ComponentName?) {
                println("disconnected")
            }
        }

        bindService(startService1,connect, BIND_AUTO_CREATE)

        unbindService(connect)

//        stopService(startService1)

        val intent =  Intent(this,MyIntentService::class.java)
        startService(intent)

        val intent2 = Intent()
        intent2.setPackage("com.fhz.a2102high6")
        intent2.action = "com.feng.bawei1"
        startActivity(intent2)

    }


    /**
     * 本地广播
     */
    @Deprecated("别用了.换一个")
    private fun localbraocast(goods: Goods) {
//        val intent = Intent(BROADCAST_ACTION)
////        intent.setAction("com")
//        val bundle = Bundle()
//        bundle.putString("name","小明")
//        bundle.putString("title",goods.goodsDesc)
//        intent.putExtras(bundle)
//        sendOrderedBroadcast(intent,null)
        //只能自己的应用收到了
        val instance = LocalBroadcastManager.getInstance(this)

        val intent = Intent(BROADCAST_ACTION)
        //        intent.setAction("com")
        val bundle = Bundle()
        bundle.putString("name", "小明")
        bundle.putString("title", goods.goodsDesc)
        intent.putExtras(bundle)
        instance.sendBroadcast(intent)
    }

    override fun onResume() {
        super.onResume()

    }

    companion object{
        fun launch(context: Context){
            val intent:Intent = Intent(context,MainActivity::class.java)
            context.startActivity(intent)
        }
    }

    //点击事件
//    fun rvItemClick(view:View){
////        when(view.id){
////            R.id.goods_item_pic -> {
////                lifecycleScope.launch {
////                    withContext(Dispatchers.IO){
////                        val goods = goodsList[0]
////                        mainViewModel.insertGoods(goods)
////                    }
////                }
////            }
////            R.id.goods_item_title -> {
////                println(2)
////            }
////        }


//    }


    override fun onDestroy() {
        super.onDestroy()
//        instance.unregisterReceiver(receiver)
        unregisterReceiver(receiver3)
    }

}